<template>
    <div class="recipes">
        <el-form :inline="true">
            <el-form-item>
                <el-button @click="$router.push({ name: 'dishes' })">返回</el-button>
                <el-button type="primary" @click="addOrUpdateHandle(0)">新增</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="dataList" border>
            <el-table-column prop="recipesId" header-align="center" align="center" label="菜谱ID">
            </el-table-column>
            <el-table-column prop="recipesDetailList.mainText" header-align="center" align="center" label="菜谱名">
            </el-table-column>
            <el-table-column prop="recipesDetailList.mainSrc" header-align="center" align="center" label="菜谱图">
                <template slot-scope="scope">
                    <img :src=scope.row.recipesDetailList.mainSrc style="width: 100px; height: 100px;">
                </template>
            </el-table-column>
            <el-table-column fixed="right" header-align="center" align="center" width="230" label="操作">
                <template slot-scope="scope">
                    <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.recipesId)">修改详情</el-button>
                    <el-button type="text" size="small" @click="deleteHandle(scope.row.recipesId)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script>
export default {
    data() {
        return {
            spuId: 0,
            dataList: [],
        }
    },
    activated() {
        this.spuId = this.$route.params.id
        this.getDataList()

    },
    methods: {
        getDataList() {
            this.$http({
                url: this.$http.adornUrl(`/web/recipes/list/${this.spuId}`),
                method: 'get',
            }).then(({ data }) => {
                this.dataList = data.list
            })
        },
        addOrUpdateHandle(id) {
            let spuId = this.spuId
            this.$router.push({ name: 'recipesaddupdate', params: { id, spuId } });
        },
        deleteHandle(id) {
            this.$confirm('确定要删除该菜谱吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http({
                    url: this.$http.adornUrl(`/web/recipes/delete/${id}`),
                    method: 'Delete',
                }).then(({ data }) => {
                    if (data && data.code === 0) {
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                            onClose: () => {
                                this.getDataList()
                            }
                        })
                    } else {
                        this.$message.error(data.msg)
                    }
                }).catch(() => { })
            })

        }
    }
}
</script>

<style></style>